package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpLazyLoadFragment;
import com.chexiehui.jc.entity.AreaForumInfo;
import com.chexiehui.jc.entity.ForumInfo;
import com.chexiehui.jc.mvp.forum.ForumTribuneAreaCovenant;
import com.chexiehui.jc.mvp.forum.ForumTribuneAreaPresenter;
import com.chexiehui.jc.ui.activity.ForumTribuneDetailsActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.chexiehui.jc.widget.LoadingLayout;
import com.chexiehui.jc.widget.indexbar.IndexBar;
import com.chexiehui.jc.widget.indexbar.suspension.SuspensionDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: SongJun.
 * CreateDate: 2019/1/21 0021.
 * Description: 论坛区域
 */

public class ForumTribuneAreaFragment extends BaseMvpLazyLoadFragment<ForumTribuneAreaPresenter> implements ForumTribuneAreaCovenant.View {

    public static ForumTribuneAreaFragment getInstance() {
        ForumTribuneAreaFragment fragment = new ForumTribuneAreaFragment();
        return fragment;
    }

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.ib_right_index_bar)
    IndexBar ibRightIndexBar;
    @BindView(R.id.tv_side_bar_hint)
    TextView tvSideBarHint;

    private CommonAdapter<ForumInfo> tribuneAreaAdapter;
    private List<ForumInfo> tribuneAreaListData;
    private SuspensionDecoration suspensionDecoration;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_forum_tribune_area;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableOverScrollDrag(true);
        refreshLayout.setDragRate(0.5f);

        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        recycler.setLayoutManager(layoutManager);
        tribuneAreaListData = new ArrayList<>();
        tribuneAreaAdapter = new CommonAdapter<ForumInfo>(mActivity, R.layout.item_forum_tribune_area, tribuneAreaListData) {
            @Override
            protected void convert(ViewHolder holder, ForumInfo item, int position) {
                holder.setText(R.id.tv_item_name, item.getName());
                GlideUtil.loadImageViewCustomerRoundCrop(mContext, item.getIcon(), holder.getView(R.id.iv_item_image),
                        R.drawable.shape_round_5_gray, R.drawable.shape_round_5_gray,
                        true, true, true, true, SizeUtils.dp2px(5));
            }
        };
        tribuneAreaAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                startActivity(ForumTribuneDetailsActivity.class);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        tribuneAreaAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(NoDoubleClickUtils.isDoubleClick()) return;
                startActivity(ForumTribuneDetailsActivity.class, new BundleBuilder()
                        .putString("forumId", tribuneAreaListData.get(position).getId())
                        .create());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(tribuneAreaAdapter);
        suspensionDecoration = new SuspensionDecoration(mActivity, tribuneAreaListData);
        recycler.addItemDecoration(suspensionDecoration);
        //indexbar初始化
        ibRightIndexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(false)//设置需要真实的索引
                .setSourceDatasAlreadySorted(true)
                .setmLayoutManager(layoutManager);//设置RecyclerView的LayoutManager
        isPrepared = true;
        lazyLoad();
    }

    @Override
    protected void lazyLoad() {
        if (!isVisible || !isPrepared || mHasLoadedOnce) {
            return;
        }
        loadingLayout.setStatus(LoadingLayout.Loading);
        mvpPresenter.getList();
    }

    @Override
    protected ForumTribuneAreaPresenter createPresenter() {
        return new ForumTribuneAreaPresenter(this);
    }

    @Override
    public void onGetListSuccess(BaseModel<List<AreaForumInfo>> successResult) {
        mHasLoadedOnce = true;
        if (loadingLayout.getStatus() == LoadingLayout.Loading) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh(true);
        }
        ibRightIndexBar.setVisibility(View.VISIBLE);
        List<ForumInfo> list = new ArrayList<>();
        for (int i = 0; i < successResult.getData().size(); i++) {
            list.addAll(successResult.getData().get(i).getForumsList());
        }
        tribuneAreaListData.clear();
        tribuneAreaListData.addAll(list);
        tribuneAreaAdapter.notifyDataSetChanged();
        ibRightIndexBar.setmSourceDatas(tribuneAreaListData)//设置数据
                .invalidate();
        suspensionDecoration.setmDatas(tribuneAreaListData);
    }

    @Override
    public void onGetListFailure(BaseModel<Object> failureResult) {
        if (loadingLayout.getStatus() == LoadingLayout.Loading) {
            loadingLayout.setStatus(LoadingLayout.Success);
        }
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh(true);
        }
        ibRightIndexBar.setVisibility(View.GONE);
        tribuneAreaListData.clear();
        tribuneAreaAdapter.notifyDataSetChanged();
        ibRightIndexBar.setmSourceDatas(tribuneAreaListData)//设置数据
                .invalidate();
        suspensionDecoration.setmDatas(tribuneAreaListData);
        if (refreshLayout.getState() == RefreshState.Refreshing) {
            refreshLayout.finishRefresh(false);
        }
    }
}
